#!/bin/bash
set -x
file_name=$0
BASH_CMD=/bin/bash
META_TAR_NAME="meta_service_v1.tar.gz"
META_DOWNLOAD_URL="https://primihub.oss-cn-beijing.aliyuncs.com/tools/${META_TAR_NAME}"
MAX_CHECK_TIMES=30
META_DIR=meta_service
META_SERVER_NAME=fusion-simple.jar
META_SERVER_PORT="7977 7978 7979"
function helper_for_install_jre8() {
  echo "jre 8 is required, please first install jre"
  echo "for ubuntu20.04, follow this instruction: sudo apt install openjdk-8-jre-headless"
  echo "for other os, you can fetch jre from https://github.com/adoptium/temurin8-binaries/releases/tag/jdk8u372-b07"
}

function check_meta_server_available() {
  for meta_port in ${META_SERVER_PORT[@]}; do
    result=$(netstat -na 2> /dev/null |grep "LISTEN" | awk '{print $4}' | grep ${meta_port} | grep -v grep)
    if [ -z "${result}" ]; then
      return 1
    fi
  done
  return 0
}

function start_meta_server() {
  echo "start_meta_server"
  #check java
  java_cmd=$(command -v java)
  if [ "${java_cmd}" == "" ]; then
    helper_for_install_jre8
    exit 1
  fi
  #check meta server is start or not
  check_meta_cmd=$(ps -ef | grep ${META_SERVER_NAME} | grep -v grep)
  if [ "${check_meta_cmd}" == "" ]; then
    set +x
    [ ! -d "${META_DIR}" ] && mkdir ${META_DIR}
    pushd ${META_DIR} &> /dev/null
      [ ! -f "${META_TAR_NAME}" ] && wget ${META_DOWNLOAD_URL}
      [ ! -d ${META_DIR} ] && tar -zxf ${META_TAR_NAME}
      echo "waiting for meta server start..., it will cost about 6 seconds"
      pushd ${META_DIR} &> /dev/null
        ${BASH_CMD} run.sh
        retry_time=0
        while :; do
          check_meta_server_available
          ret_code=$?
          if [ $ret_code -eq 0 ]; then
            break
          else
            if [ ${retry_time} -gt ${MAX_CHECK_TIMES} ]; then
              echo "some thine wrong with starting meta service"
              echo "log is located in ${pwd}"
              exit 1
            fi
            sleep 1
            ((retry_time++))
            continue
          fi
        done
      popd &> /dev/null
    popd &> /dev/null
    set -x
  fi
}

# start primihub server
function start_primihub_server() {
  file_dir=$(dirname $(readlink -f ${file_name}))
  echo ${file_dir}
  py_primihub_path="${file_dir}/python"
  if [ ! -d "${py_primihub_path}" ]; then
    echo "${py_primihub_path} is not exists, please check ..."
    exit 1
  fi
  #check the exists of file generated by proto
  proto_file=${py_primihub_path}/primihub/client/ph_grpc/src/primihub/protos/common_pb2.py
  if [ -f "${proto_file}" ]; then
    export PYTHONPATH=${PYTHONPATH}:${py_primihub_path}
  fi
  PYTHON_BIN=python3
  if ! command -v python3 >/dev/null 2>&1; then
    if ! command -v python >/dev/null 2>&1; then
      echo "please install python3"
      exit
    else
      PYTHON_BIN=python
    fi
  fi
  U_V1=`$PYTHON_BIN -V 2>&1|awk '{print $2}'|awk -F '.' '{print $1}'`
  U_V2=`$PYTHON_BIN -V 2>&1|awk '{print $2}'|awk -F '.' '{print $2}'`
  U_V3=`$PYTHON_BIN -V 2>&1|awk '{print $2}'|awk -F '.' '{print $3}'`

  echo your python version is : "$U_V1.$U_V2.$U_V3"
  PYTHON_CONFIG_CMD="python$U_V1.$U_V2-config"
  prefix_path=$(${PYTHON_CONFIG_CMD} --prefix)
  export LD_LIBRARY_PATH=${prefix_path}/lib:${LD_LIBRARY_PATH}
  # log_level 1->7,the larger the value the more detailed the log
  log_level=7
  export GLOG_logtostderr=1 GLOG_v=${log_level}
  nohup ./bazel-bin/node --node_id=node0 --config=./config/node0.yaml >> log_node0 2>&1 &
  nohup ./bazel-bin/node --node_id=node1 --config=./config/node1.yaml >> log_node1 2>&1 &
  nohup ./bazel-bin/node --node_id=node2 --config=./config/node2.yaml >> log_node2 2>&1 &
}

function main() {
  start_meta_server
  start_primihub_server
}

main
